﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using PPI.Entities.Interface;
using PPI.Entities.Lookup;
using PPI.Mobile;
using VMind.Global;
using PPIDataAccess.Distribution;

namespace PPI.ServiceLogic.Distribution
{
    public class LTop : IMobileTable
    {
        public static DataSet SelectAllIdName(string connectionKey)
        {
            return DTops.SelectAll(connectionKey);            
        }

        public static DataSet SelectAllIdName(string connectionKey, Int32 fromId)
        {
            return DTops.SelectAll(connectionKey, fromId);
        }

        public static Int32 Insert(EXMissionStructures_PoleTopType top, Int32 poleId, string connectionString)
        {
            return DTops.Insert(top, poleId, connectionString);
        }

        public static void Update(List<EXMissionStructures_PoleTopType> tops, Int32 poleId, string connectionString)
        {
            DTops.Update(tops, poleId, connectionString);
        }

        public List<EMobileQuery> CreateInsertMobileQuery(string connectionKey)
        {
            DataTable tops = DTops.SelectAll(connectionKey).Tables[0];

            List<EMobileQuery> listQueries = new List<EMobileQuery>();
            listQueries.Add(new EMobileQuery(string.Format("DELETE tb_Lookups WHERE LookupTypeFk = '{0}'", "Top")));

            EMobileQuery insertQuery = new EMobileQuery("INSERT INTO tb_Lookups (LookupValue, LookupText, LookupTypeFk) VALUES (@id, @Name, 'Top')");

            foreach (DataRow top in tops.Rows)
            {
                List<SqlParameter> parameters = new List<SqlParameter>();

                parameters.Add(DBHelper.CreateIntParameter("@id", ParameterDirection.Input, false, Convert.ToInt32(top["Distributions_TopTypeId"])));
                parameters.Add(DBHelper.CreateNVarCharParameter("@Name", 100, ParameterDirection.Input, false, top["Name"]));

                insertQuery.Parameters.Add(parameters);
            }

            listQueries.Add(insertQuery);
            
            return listQueries;
        }
    }
}
